/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 8-set-2011, 15.05.21
 */
package sputty.gui;

import ch.ethz.ssh2.Session;
import java.awt.LayoutManager;
import java.awt.ScrollPane;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.tree.DefaultMutableTreeNode;
import sputty.classes.ConnectionSettings;
import sputty.classes.MyTerminalArea;
import sputty.classes.ParseConfiguration;
import sputty.classes.RemoteConnection;
import sputty.classes.SendCommands;

/**
 *
 * @author mauro
 */
public class MainFrame extends javax.swing.JFrame {

    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
        try{
            setIconImage(ImageIO.read(this.getClass().getResourceAsStream("/sputty/gui/resources/terminal.png")));
        }catch(Exception e){
            e.printStackTrace();
        }
        Split.setDividerLocation((this.getWidth()/5)*4);
        new ParseConfiguration(treeConnections).run();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mnuConnSettings = new javax.swing.JPopupMenu();
        mnuProprieta = new javax.swing.JMenuItem();
        Split = new javax.swing.JSplitPane();
        PTerminal = new javax.swing.JPanel();
        TabConnections = new javax.swing.JTabbedPane();
        PConnections = new javax.swing.JPanel();
        SPTree = new javax.swing.JScrollPane();
        treeConnections = new javax.swing.JTree();
        mnuBar = new javax.swing.JMenuBar();
        mnuGen = new javax.swing.JMenu();
        mnuConnect = new javax.swing.JMenuItem();
        mnuAbout = new javax.swing.JMenu();

        mnuConnSettings.setFont(new java.awt.Font("Dialog", 1, 10)); // NOI18N
        mnuConnSettings.setInvoker(treeConnections);

        mnuProprieta.setFont(new java.awt.Font("Dialog", 1, 10)); // NOI18N
        mnuProprieta.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sputty/gui/resources/popupmenus/configure.png"))); // NOI18N
        mnuProprieta.setText("Proprietà");
        mnuProprieta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuProprietaActionPerformed(evt);
            }
        });
        mnuConnSettings.add(mnuProprieta);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setFont(new java.awt.Font("Dialog", 1, 10));
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        Split.setPreferredSize(new java.awt.Dimension(800, 600));

        PTerminal.setPreferredSize(new java.awt.Dimension(500, 800));
        PTerminal.setLayout(new java.awt.BorderLayout());
        PTerminal.add(TabConnections, java.awt.BorderLayout.CENTER);

        Split.setLeftComponent(PTerminal);

        PConnections.setPreferredSize(new java.awt.Dimension(300, 800));
        PConnections.setLayout(new java.awt.BorderLayout());

        treeConnections.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                treeConnectionsMouseClicked(evt);
            }
        });
        SPTree.setViewportView(treeConnections);

        PConnections.add(SPTree, java.awt.BorderLayout.CENTER);

        Split.setRightComponent(PConnections);

        getContentPane().add(Split, java.awt.BorderLayout.CENTER);

        mnuGen.setText("File");
        mnuGen.setFont(new java.awt.Font("Dialog", 1, 10));

        mnuConnect.setFont(new java.awt.Font("Dialog", 1, 10));
        mnuConnect.setText("Connetti");
        mnuConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuConnectActionPerformed(evt);
            }
        });
        mnuGen.add(mnuConnect);

        mnuBar.add(mnuGen);

        mnuAbout.setText("About");
        mnuAbout.setFont(new java.awt.Font("Dialog", 1, 10));
        mnuBar.add(mnuAbout);

        setJMenuBar(mnuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnuConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuConnectActionPerformed
        
    }//GEN-LAST:event_mnuConnectActionPerformed

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        Split.setDividerLocation((this.getWidth()/5)*4);
    }//GEN-LAST:event_formComponentResized

    private void treeConnectionsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_treeConnectionsMouseClicked
        if(evt.getClickCount() == 2){
            DefaultMutableTreeNode Selected = (DefaultMutableTreeNode)treeConnections.getLastSelectedPathComponent();
            if(Selected.isLeaf()){
                ConnectionSettings ConSet = (ConnectionSettings)Selected.getUserObject();
                RemoteConnection Remote = new RemoteConnection(this, TabConnections, ConSet);
                Remote.run();
                
                if(ConSet.getPostCommands()){
                    if(ConSet.getCommands().length > 0){
                        new SendCommands(Remote.getInputStream(), Remote.getOutputStream(), ConSet.getCommands(), ConSet.getCommandTimeOut(),true).run();
                    }
                }
            }
        }
        if(evt.getButton() == evt.BUTTON3){
            int ToSelect = treeConnections.getClosestRowForLocation(evt.getX(), evt.getY());
            treeConnections.setSelectionRow(ToSelect);
            DefaultMutableTreeNode Selected = (DefaultMutableTreeNode)treeConnections.getLastSelectedPathComponent();
            if(Selected.isLeaf()){
                mnuConnSettings.setLocation(evt.getLocationOnScreen().x, evt.getLocationOnScreen().y);
                mnuConnSettings.setVisible(true);
            }else{
                
            }
        }
    }//GEN-LAST:event_treeConnectionsMouseClicked

    private void mnuProprietaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuProprietaActionPerformed
        DefaultMutableTreeNode Selected = (DefaultMutableTreeNode)treeConnections.getLastSelectedPathComponent();
        if(Selected.isLeaf()){
            frmConnectionsProperties F = new frmConnectionsProperties((ConnectionSettings)Selected.getUserObject());
            F.setVisible(true);
            mnuConnSettings.setVisible(false);
        }else{
            
        }
    }//GEN-LAST:event_mnuProprietaActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel PConnections;
    private javax.swing.JPanel PTerminal;
    private javax.swing.JScrollPane SPTree;
    private javax.swing.JSplitPane Split;
    private javax.swing.JTabbedPane TabConnections;
    private javax.swing.JMenu mnuAbout;
    private javax.swing.JMenuBar mnuBar;
    private javax.swing.JPopupMenu mnuConnSettings;
    private javax.swing.JMenuItem mnuConnect;
    private javax.swing.JMenu mnuGen;
    private javax.swing.JMenuItem mnuProprieta;
    private javax.swing.JTree treeConnections;
    // End of variables declaration//GEN-END:variables
}
